#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    QIcon icon(":/FenNi.jpg");

    ui->pushButton->setIcon(icon);
    ui->pushButton->setIconSize(QSize(150,150));

    //直接通过按键的名字
    ui->up->setShortcut(QKeySequence("w"));
    ui->down->setShortcut(QKeySequence("s"));
    ui->left->setShortcut(QKeySequence("a"));
    ui->right->setShortcut(QKeySequence("d"));

    //使用按键的枚举
//    ui->up->setShortcut(QKeySequence(Qt::Key_Up));
//    ui->down->setShortcut(QKeySequence(Qt::Key_Down));
//    ui->left->setShortcut(QKeySequence(Qt::Key_Left));
//    ui->right->setShortcut(QKeySequence(Qt::Key_Right));

    //键盘默认自带连发
    //开启鼠标连发
    ui->up->setAutoRepeat(true);
    ui->down->setAutoRepeat(true);
    ui->left->setAutoRepeat(true);
    ui->right->setAutoRepeat(true);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_up_clicked()
{
    QRect rect = ui->pushButton->geometry();
    ui->pushButton->setGeometry(rect.x(),rect.y()-5,rect.width(),rect.height());
}

void Widget::on_down_clicked()
{
    QRect rect = ui->pushButton->geometry();
    ui->pushButton->setGeometry(rect.x(),rect.y()+5,rect.width(),rect.height());
}

void Widget::on_left_clicked()
{
    QRect rect = ui->pushButton->geometry();
    ui->pushButton->setGeometry(rect.x()-5,rect.y(),rect.width(),rect.height());
}

void Widget::on_right_clicked()
{
    QRect rect = ui->pushButton->geometry();
    ui->pushButton->setGeometry(rect.x()+5,rect.y(),rect.width(),rect.height());
}
